<?php

namespace App\Http\Controllers\Clbum;

use App\Criteria\Clbum\Clbum\ClbumExtBaseQueryCriteria;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CurlRecordMethod;
use App\Http\Requests\Clbum\ClbumBaseRequest;
use App\Libs\Tools\SystemCodeConstant;
use App\Repositories\Clbum\ClbumExtRepository;
use App\Repositories\Clbum\ClbumRepository;
use App\Repositories\Student\StudentRepository;
use App\Repositories\System\SystemCodeRepository;
use App\Repositories\System\UserRepository;
use App\Validators\Clbum\ClbumExtValidator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;


class ClbumExtsController extends Controller
{
    use CurlRecordMethod;

    protected $edit_page='clbum.clbum.clbumExtEdit';

    protected $repository;
    protected $validator;
    protected $request;
    protected $userRepository;
    protected $clbumRepository;
    protected $systemCodeRepository;
    protected $studentRepository;

    public function __construct(ClbumExtRepository $repository,
                                  ClbumExtValidator $validator,
                                  ClbumBaseRequest $request,
                                  UserRepository $userRepository,
                                  ClbumRepository $clbumRepository,
                                  SystemCodeRepository $systemCodeRepository,
                                  StudentRepository $studentRepository)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
        $this->request = $request;
        $this->userRepository =$userRepository;
        $this->clbumRepository =$clbumRepository;
        $this->systemCodeRepository = $systemCodeRepository;
        $this->studentRepository = $studentRepository;
    }


    public function getBaseQueryCriteria(){
        return ClbumExtBaseQueryCriteria::class;
    }

    public function get_edit_data()
    {
        $clbum_id=Input::get('clbum_id');
        $course_leader_type=SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE;
        $course_teacher_type=SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE;
        $student_leader_type=SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE;

        $course_leaders=$this->repository->pushCriteria($this->getBaseQueryCriteria())->findWhere([
            ['clbum_id','=',$clbum_id],
            ['system_code_type','=',$course_leader_type]
        ]);
        $course_teachers=$this->repository->resetCriteria()->pushCriteria($this->getBaseQueryCriteria())->findWhere([
            ['clbum_id','=',$clbum_id],
            ['system_code_type','=',$course_teacher_type]
        ]);
        $student_leaders=$this->repository->resetCriteria()->pushCriteria($this->getBaseQueryCriteria())->findWhere([
            ['clbum_id','=',$clbum_id],
            ['system_code_type','=',$student_leader_type]
        ]);

        $users=$this->userRepository->all();
        $students=$this->studentRepository->findByField('clbum_id',$clbum_id);
        return view($this->edit_page,compact('course_leaders','course_teachers','student_leaders','users','students','clbum_id'));
    }

    public function post_edit_data()
    {
        DB::transaction(function () {
            $course_leaders=Input::get(SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE);
            $course_teachers=Input::get(SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE);
            $student_leaders=Input::get(SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE);
            $clbum_id=Input::get('clbum_id');

            $this->update_clbum_ext(SystemCodeConstant::CLBUM_EXT_COURSE_LEADER_TYPE,$course_leaders,$clbum_id);
            $this->update_clbum_ext(SystemCodeConstant::CLBUM_EXT_COURSE_TEACHER_TYPE,$course_teachers,$clbum_id);
            $this->update_clbum_ext(SystemCodeConstant::CLBUM_EXT_STUDENT_LEADER_TYPE,$student_leaders,$clbum_id);
        });
        return $this->return_json_success();
    }

    private function update_clbum_ext($type,$values,$clbum_id){
        foreach ($values as $key=>$value){
            DB::update('update clbum_ext set value = ?,updated_user=?,updated_at=? 
                    where clbum_id = ? and system_code_type=? and system_code_value=?',
                    [$value,Auth::user()['name'],date('Y-m-d H:i:s'),$clbum_id,$type,$key]);
        }

    }
}
